package goodsManagement;

public class TypeManagement {
	Type[] types;
	
	public TypeManagement(){
		this.types = new Type[10];
	}
	
	private int getUseFulIndex(){
		for(int i = 0 ; i < types.length; i++){
			if(null == types[i]){
				return i;
			}
		}
		return -1;
	}
	
	public boolean save(Type type){
		int index = getUseFulIndex();
		if(-1 == index){
			return false;
		}
		types[index] = type;
		return true;
	}
	
	public boolean delete(Type type){
		for (int i = 0; i < types.length && types[i]!=null; i++) {
			if(types[i].getTypeId() == type.getTypeId()){
				int j;
				for (j = i+1; j < types.length && types[j]!=null ; j++) {
					types[j-1] = types[j];
				}
				types[j-1] = null;
				return true;
			}
		}
		return false;
	}
	
	public boolean update(Type type){
		for (int i = 0; i < types.length && types[i]!=null; i++) {
			if(types[i].getTypeId() == type.getTypeId()){
				types[i].setTypeName(type.getTypeName());
				return true;
			}
		}
		return false;
	}
	
	public Type selectById(int typeId){
		for (int i = 0; i < types.length && types[i]!=null; i++) {
			if(types[i].getTypeId() == typeId ){
				return types[i];
			}
		}
		return null;
	}
	
	public Type selectByName(String typeName){
		for (int i = 0; i < types.length && types[i]!=null; i++) {
			if(types[i].getTypeName().equals(typeName)){
				return types[i];
			}
		}
		return null;
	}
	
	public Type[] selectAll(){
		return types;
	}
}
